const _ = require('lodash');
const fse = require('fs-extra');
const handlebars = require('handlebars');
const moment = require('moment');

const { minify } = require('html-minifier');

handlebars.registerHelper('date', (value) => moment(value).format('YYYY-MM-DD'));
handlebars.registerHelper('join', (value, separator) => _.join(value, separator));
handlebars.registerHelper('safestring', (value) => new handlebars.SafeString(value));

class Renderer {
  constructor(templatePath) {
    const fileContent = fse.readFileSync(templatePath).toString();
    this.compile = handlebars.compile(fileContent);
  }

  render(data) {
    return minify(this.compile(data), { collapseWhitespace: true });
  }
}

module.exports = Renderer;
